PROGRAM RANDOM1;{Bubblesort as the sort method}
CONST MAXSIZE=500;
VAR SEED:REAL;
DIDSWAP:BOOLEAN;
TIMEDIFF,HITIME,LO1TIME,LO2TIME,X,Y:INTEGER;
SCALAR:ARRAY[1..MAXSIZE] OF INTEGER;

PROCEDURE SWAP (VAR I,J:INTEGER);
VAR TEMP:INTEGER;
BEGIN
   TEMP:=I;
   I:=J;
   J:=TEMP;
END;

PROCEDURE SORT;
VAR COUNTER:INTEGER;
BEGIN
DIDSWAP:=FALSE;
   FOR COUNTER:=1 TO MAXSIZE-1 DO
   BEGIN
      IF SCALAR[COUNTER]>SCALAR[COUNTER+1] THEN
         BEGIN
         SWAP(SCALAR[COUNTER],SCALAR[COUNTER+1]);
         DIDSWAP:=TRUE;
         END;
   END;
END;

FUNCTION RANDOM:INTEGER;
BEGIN
SEED:=SEED*27.182813+31.415917;
SEED:=SEED-TRUNC(SEED);
IF SEED<0 THEN
   SEED:=-SEED;
RANDOM:=TRUNC(SEED*50+1);
END;

BEGIN
TIME(HITIME,LO1TIME);
SEED:=1.23456789;
FOR X:=1 TO MAXSIZE DO
   SCALAR[X]:=RANDOM;
FOR Y:=1 TO MAXSIZE DO
   WRITE(SCALAR[Y]:4);
X:=1;
REPEAT
   BEGIN
   SORT;
   WRITELN('IN ITERATION ',X,' OF SWAP-REPEAT LOOP');
   X:=X+1;
   END;
UNTIL DIDSWAP=FALSE;
FOR Y:=1 TO MAXSIZE DO
   WRITE(SCALAR[Y]:4);
TIME(HITIME,LO2TIME);
TIMEDIFF:=LO2TIME-LO1TIME;
IF TIMEDIFF<0 THEN
   TIMEDIFF:=TIMEDIFF+32767;
WRITELN(TIMEDIFF DIV 60,'.',(100*(TIMEDIFF MOD 60) DIV 60),' seconds');
END.
